package com.slj.controller;

import com.slj.comm.responce.PageResult;
import com.slj.comm.responce.R;
import com.slj.model.query.ProductQuery;
import com.slj.model.entity.Products;
import com.slj.model.vo.ProductVo;
import com.slj.service.ProductsService;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 供应商商品信息表 前端控制器
 * </p>
 *
 * @author 礼俊
 * @since 2025-09-20 22:46
 */
@RestController
@RequestMapping("/products")
@Tag(name = "供应商商品信息表")
public class ProductsController {
    private static final Logger log = LoggerFactory.getLogger(ProductsController.class);
    @Autowired
    private ProductsService productsService;
    //查询商品
    @PostMapping("/list")
    public R<PageResult<ProductVo>> list(ProductQuery productQuery) {
        PageResult<ProductVo> vo = productsService.pageList(productQuery);
        log.info("分页查询供应商商品信息：{}", vo);
        return R.success(vo) ;
    }
    //编辑商品
    @PostMapping("/edit")
    @Schema(description = "编辑商品")
    @Parameter(name = "products", description = "商品信息")
    public R editProduct(Products products, MultipartFile imageFile) {
        productsService.editProduct(products, imageFile);
        return R.success();
    }

    //分页查询获取商品列表
    @PostMapping("/page")
    @Schema(description = "分页查询获取商品列表")
    @Parameter(name = "productQuery", description = "商品查询参数")
    public R<PageResult<ProductVo>> getProductPage(ProductQuery productQuery) {
        PageResult<ProductVo> productsPage = productsService.getProductPage(productQuery);
        log.info("分页查询获取商品列表：{}", productsPage);
        return R.success(productsPage);
    }

    //根据id删除商品
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {
        return productsService.removeById(id) ? R.success() : R.fail("删除失败");
    }

    //根据id查询商品
    @GetMapping("/get/{id}")
    public R<Products> get(@PathVariable Integer id) {
        Products products = productsService.getById(id);
        return R.success(products);
    }

}
